/**
 * Created by zzc on 2017/9/13.
 */
import React, {Component} from 'react';

import {
    View,
    Platform,
    BackHandler
} from 'react-native';
import {Actions} from "react-native-router-flux";
import BaseView from "./widget/BaseView";
import {PAGE_LOADING, PageStatus} from "./Constants";
import HttpServer from "./http/HttpServer";
import NetWorkUtil from "./util/NetWorkUtil";
import StringUtil from "./util/StringUtil";


export default class BaseScene extends React.Component {

    constructor(props){
        super(props)
        this.state={
            status:PageStatus.SUCCESS,
            errorText: ""
        }
    }

    componentWillMount() {
        if (Platform.OS === 'android') {
            BackHandler.addEventListener('hardwareBackPress', this._onBackAndroid);
        }
    }

    componentWillUnmount() {
        if (Platform.OS === 'android') {
            BackHandler.removeEventListener('hardwareBackPress', this._onBackAndroid);
        }
    }

    _onBackAndroid = () => {
        if(Actions.currentScene == "main"){
            return false;
        }else{
            Actions.pop();
            HttpServer.cancelRequest()
            return true;
        }
    };

    /******获取子类的view************/
    _getChildView(){

    }

    /*************是否显示标题*********/
    _setTitle(){

    }

    /*************轻点刷新*********/
    _onReloadClick=()=>{

    }

    /*************顶部提示框*********/
    _showTopTip=(msg)=>{

    }

    _requestSuccess=(response)=>{

    }

    _requestFailed=(response)=>{

    }

    _fetchRequest=(opts,params)=>{
        let nowTime = (new Date()).valueOf();
        console.log("*****_fetchRequest******"+nowTime)
        this._dealRequestData(opts,null,PageStatus.LOADING,params)
        HttpServer.httpServer(opts,params)
            .then((response)=>{
                console.log(response)
                this._dealRequestData(opts,response,PageStatus.SUCCESS,params)
            },(error)=>{
                NetWorkUtil.checkNetworkState((isConnected)=>{
                    if(!isConnected){
                        this._dealRequestData(opts,error,PageStatus.NETWORK_ERROR,params)
                    }else{
                        this._dealRequestData(opts,error,PageStatus.ERROR,params)
                    }
                })
            })
    }

    _dealRequestData =(opts,response,status,params)=>{
        if(opts.loading === PAGE_LOADING || (opts.isList === true && opts.isFirstIn === true)){
            switch (status){
                case PageStatus.LOADING:
                case PageStatus.EMPTY:
                case PageStatus.NETWORK_ERROR:
                    this.setState({
                        status: status
                    })
                    break
                case PageStatus.SUCCESS:
                    this.setState({
                        status: status
                    })
                    this._requestSuccess(response)
                    break
                case PageStatus.ERROR:
                    if(opts.missError === true){
                        this._requestFailed(response)
                    }else{
                        this.setState({
                            status: status,
                            errorText :StringUtil.isNotEmpty(response.msg)?response.msg:"对不起，出错了"
                        })
                    }
                    break
            }
        }else{
            switch (status){
                case PageStatus.SUCCESS:
                    this._requestSuccess(response)
                    break
                case PageStatus.ERROR:
                    this._requestFailed(response)
                    break
            }
        }
    }

    render() {
        return (
            <BaseView
                status={this.state.status}
                emptyText="您还没有添加数据"
                errorText={this.state.errorText}
                onErrorPress = {this._onReloadClick.bind(this)}>

                {this._getChildView()}

            </BaseView>
        );
    }
}
